package com.example.netty.chatSystem.handler;

import com.example.netty.chatSystem.channelService.ChannelService;
import com.example.netty.chatSystem.login.Login;
import com.example.netty.chatSystem.message.LoginMessage;
import com.example.netty.chatSystem.message.LoginResult;
import io.netty.channel.*;

import java.util.concurrent.ConcurrentHashMap;
@ChannelHandler.Sharable
public class MyChannelInboundHandlerAdapter extends SimpleChannelInboundHandler<LoginMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginMessage loginMessage) throws Exception {
        String name = loginMessage.getName();
        String password = loginMessage.getPassword();
        boolean isLogin = Login.isLongin(name, password);
        if (isLogin) {
            ctx.writeAndFlush(new LoginResult(true, "欢迎登录"));
            ConcurrentHashMap<String, Channel> hashMap = ChannelService.getHashMap();
            hashMap.put(name, ctx.channel());
        } else {
            ctx.writeAndFlush(new LoginResult(false, "账号或用户名错误"));
        }
    }
}
